package com.weixin.Controller;

import com.weixin.bean.MessageBean;
import com.weixin.dao.IMessageDao;
import com.weixin.dao.ISendMessageDao;
import com.weixin.servlet.IMessageServlet;
import com.weixin.servlet.ISendMessageKeyServlet;
import com.weixin.utils.CheckUtil;
import com.weixin.utils.MessageUtils;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2018-01-15.
 */
@Controller
@RequestMapping("index")
public class WechatController {

    @Autowired
    private IMessageServlet messageServlet ;

    @Autowired
    private ISendMessageKeyServlet sendMessageKeyServlet ;


    @RequestMapping("")
    private void init(HttpServletRequest request, HttpServletResponse response, String signature, String timestamp, String nonce , String echostr) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = null ;
        MessageBean  messageBean = new MessageBean();
        try {
             out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String str = "";
        //接入验证
        if(null!=signature&&null!=timestamp&&null!=nonce) {
            if (CheckUtil.checkSign(signature, timestamp, nonce)) {
                out.print(echostr);
                return;
            }
        }
        try {
                //将request请求，传到Message工具类的转换方法中，返回接收到的Map对象

            Map<String, String> map = MessageUtils.xmlToMap(request);

            messageBean.setMsgType(map.get("MsgType"));
                /**对用户信息进行处理*/
            if(messageBean.getMsgType().equals("text")) {//判断消息类型是否是文本消息(text)
                str = textMessage(messageBean, map);
            }else if(messageBean.getMsgType().equals("image")){
                imageMessage(messageBean,map);
            }else if(messageBean.getMsgType().equals("voice")){
                voiceMessage(messageBean,map);
            }else if(messageBean.getMsgType().equals("video")){
                videoMessage(messageBean,map);
            }else if(messageBean.getMsgType().equals("location")){
                locationMessage(messageBean,map);
            }else if(messageBean.getMsgType().equals("link")){
                linkMessage(messageBean,map);
            }

            if(null!=map.get("event")){

            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        out.print(str); //返回转换后的XML字符串

        out.close();
    }

    private String textMessage(MessageBean messageBean,Map<String,String> map){

        //从集合中，获取XML各个节点的内容

        messageBean.setToUserName(map.get("ToUserName"));

        messageBean.setFromUserName(map.get("FromUserName"));

        messageBean.setCreateTime(map.get("CreateTime"));

        messageBean.setMsgType(map.get("MsgType"));

        messageBean.setContent(map.get("Content"));

        messageBean.setMsgId(map.get("MsgId"));
        messageServlet.saveReceive(messageBean);

        //TODO 判断
        String str = sendMessageKeyServlet.sendMessage(messageBean);



        return str ;
    }


    private String imageMessage(MessageBean messageBean, Map<String,String> map){
        //从集合中，获取XML各个节点的内容

        messageBean.setToUserName(map.get("ToUserName"));

        messageBean.setFromUserName(map.get("FromUserName"));

        messageBean.setCreateTime(map.get("CreateTime"));

        messageBean.setMsgType(map.get("MsgType"));

        messageBean.setContent(map.get("PicUrl"));

        messageBean.setMsgId(map.get("MsgId"));

        messageServlet.saveReceive(messageBean);

        //TODO 消息接收 进行储存  未回复
        return   null ;
    }

    private String voiceMessage(MessageBean messageBean,Map<String,String> map){
        //从集合中，获取XML各个节点的内容

        messageBean.setToUserName(map.get("ToUserName"));

        messageBean.setFromUserName(map.get("FromUserName"));

        messageBean.setCreateTime(map.get("CreateTime"));

        messageBean.setMsgType(map.get("MsgType"));

        String MediaId = map.get("MediaId");
        String Format = map.get("Format");
        messageBean.setContent(MediaId +";"+Format);

        messageBean.setMsgId(map.get("MsgId"));

        messageServlet.saveReceive(messageBean);

        //TODO 消息接收 进行储存  未回复
        return   null ;
    }

    private String videoMessage(MessageBean messageBean,Map<String,String> map){
        //从集合中，获取XML各个节点的内容

        messageBean.setToUserName(map.get("ToUserName"));

        messageBean.setFromUserName(map.get("FromUserName"));

        messageBean.setCreateTime(map.get("CreateTime"));

        messageBean.setMsgType(map.get("MsgType"));

        String MediaId = map.get("MediaId");
        String ThumbMediaId = map.get("ThumbMediaId");
        messageBean.setContent(MediaId +";"+ThumbMediaId);

        messageBean.setMsgId(map.get("MsgId"));

        messageServlet.saveReceive(messageBean);

        //TODO 消息接收 进行储存  未回复
        return   null ;
    }

    private String locationMessage(MessageBean messageBean, Map<String,String> map){
        //从集合中，获取XML各个节点的内容

        messageBean.setToUserName(map.get("ToUserName"));

        messageBean.setFromUserName(map.get("FromUserName"));

        messageBean.setCreateTime(map.get("CreateTime"));

        messageBean.setMsgType(map.get("MsgType"));

        String  Location_X = map.get("Location_X");
        String  Location_Y = map.get("Location_Y");
        String  Scale  = map.get("Scale");
        String  Label = map.get("Label");

        messageBean.setContent(Location_X+";"+Location_Y+";"+Scale+";"+Label);

        messageBean.setMsgId(map.get("MsgId"));

        messageServlet.saveReceive(messageBean);

        //TODO 消息接收 进行储存  未回复
        return   null ;
    }

    private String linkMessage(MessageBean messageBean, Map<String,String> map){
        //从集合中，获取XML各个节点的内容

        messageBean.setToUserName(map.get("ToUserName"));

        messageBean.setFromUserName(map.get("FromUserName"));

        messageBean.setCreateTime(map.get("CreateTime"));

        messageBean.setMsgType(map.get("MsgType"));

        String  Title  = map.get("Title");
        String  Description  = map.get("Description");
        String  Url   = map.get("Url");

        messageBean.setContent(Title+";"+Description+";"+Url);

        messageBean.setMsgId(map.get("MsgId"));

        messageServlet.saveReceive(messageBean);

        //TODO 消息接收 进行储存  未回复
        return   null ;
    }


    //TODO
    private String eventMessage(MessageBean messageBean, Map<String,String> map){
        //从集合中，获取XML各个节点的内容

        messageBean.setToUserName(map.get("ToUserName"));

        messageBean.setFromUserName(map.get("FromUserName"));

        messageBean.setCreateTime(map.get("CreateTime"));

        messageBean.setMsgType(map.get("MsgType"));

        String  Title  = map.get("Title");
        String  Description  = map.get("Description");
        String  Url   = map.get("Url");

        messageBean.setContent(Title+";"+Description+";"+Url);

        messageBean.setMsgId(map.get("MsgId"));

        messageServlet.saveReceive(messageBean);

        //TODO 消息接收 进行储存  未回复
        return   null ;
    }
}
